package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class FindText {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		File f = new File(args[0]);
		File[] files = f.listFiles();
		String text = args[1];
		FileOutputStream out = new FileOutputStream("./found.txt");
		for(File file : files){
			String name = file.getName();
			if(name.indexOf("log.txt") > -1 && name.indexOf("2012") > -1){
				System.out.println("search from : " + file.getName());
				FileReader in = new FileReader(file);
				LineNumberReader lr = new LineNumberReader(in);
				String line = null;
				while((line = lr.readLine()) != null){
				 	if(line.indexOf(text) > -1){
				 		System.out.println("found in : " + file.getName());
				 		out.write(("found in : " + file.getName()).getBytes());
				 		break;
				 	}
				}
				out.close();
				try{
					lr.close();
					in.close();
				}catch(Exception e){
					System.out.println(e.getMessage());
				}
			}
		}
	}

}
